//
//  CLSelectTableViewController.m
//  togetherLBS
//
//  Created by Phyllis Hu on 14-7-3.
//  Copyright (c) 2014年 celi. All rights reserved.
//

#import "CLSelectTableViewController.h"

@interface CLSelectTableViewController ()

@end

@implementation CLSelectTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithDelegate:(id<CLSelectDelegate>)theDelegate sourceData:(NSArray *)srcArray andIndexPath:(NSIndexPath *)oneIndex
{
    self = [super initWithStyle:UITableViewStylePlain];
    if (self) {
        _dataArray = srcArray;
        delegate = theDelegate;
        if (oneIndex.row < [srcArray count]) {
            markIndexPath = oneIndex;
        }
    }
    return self;
}

- (id)initWithDelegate:(id<CLSelectDelegate>)theDelegate sourceData:(NSArray *)srcArray descriptions:(NSArray *)desArray andIndexPath:(NSIndexPath *)oneIndex
{
    self = [super initWithStyle:UITableViewStylePlain];
    if (self) {
        _dataArray = srcArray;
        delegate = theDelegate;
        if (oneIndex.row < [srcArray count]) {
            markIndexPath = oneIndex;
        }
        if ([desArray count] == [srcArray count]) {
            tipArray = desArray;
        }
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    
    if (tipArray) {
        self.tableView.rowHeight = 80;
    }
    
    if (delegate && [delegate respondsToSelector:@selector(selectTableViewControllerDidFinished:)]) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:CLLocalizedString(@"Save", nil) style:UIBarButtonItemStyleBordered target:self action:@selector(submitBtClick:)];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)submitBtClick:(id)sender
{
    [delegate selectTableViewControllerDidFinished:self];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    if (nil == tipArray) {
        static NSString *defaultCellIdentifier = @"defaultCell";
        cell = [tableView dequeueReusableCellWithIdentifier:defaultCellIdentifier];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:defaultCellIdentifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        cell.textLabel.text = [_dataArray objectAtIndex:indexPath.row];
        
    }else{
        static NSString *tipCellIdentifier = @"CLTipCell";
        cell = [tableView dequeueReusableCellWithIdentifier:tipCellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:tipCellIdentifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            CGFloat contentWidth = self.view.bounds.size.width - 40;
            UILabel *titleLb = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, contentWidth - 10, 21)];
            titleLb.backgroundColor = [UIColor clearColor];
            titleLb.font = [UIFont boldSystemFontOfSize:15.0];
            titleLb.tag = 31;
            
            UILabel *descripLb = [[UILabel alloc] initWithFrame:CGRectMake(10, 31, contentWidth -10, 40)];
            descripLb.backgroundColor = [UIColor clearColor];
            descripLb.textColor = [UIColor grayColor];
            descripLb.font = [UIFont systemFontOfSize:12.0];
            descripLb.numberOfLines = 2;
            descripLb.lineBreakMode = NSLineBreakByCharWrapping;
            descripLb.tag = 32;
            descripLb.adjustsFontSizeToFitWidth = YES;
            
            [cell.contentView addSubview:titleLb];
            [cell.contentView addSubview:descripLb];
        }
        
        UILabel *titleLb = (UILabel *)[cell.contentView viewWithTag:31];
        UILabel *descripLb = (UILabel *)[cell.contentView viewWithTag:32];
        
        titleLb.text = [_dataArray objectAtIndex:indexPath.row];
        descripLb.text = [tipArray objectAtIndex:indexPath.row];
    }
    
    if (markIndexPath && markIndexPath.row == indexPath.row) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (markIndexPath && markIndexPath.row == indexPath.row) {
        return;
    }
    if (markIndexPath) {
        UITableViewCell *preMarkCell = [tableView cellForRowAtIndexPath:markIndexPath];
        preMarkCell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    markIndexPath = indexPath;
    UITableViewCell *newMarkCell = [tableView cellForRowAtIndexPath:markIndexPath];
    newMarkCell.accessoryType = UITableViewCellAccessoryCheckmark;
    
    if ([delegate respondsToSelector:@selector(selectTableViewController:didSelectedIndexPath:)]) {
        [delegate selectTableViewController:self didSelectedIndexPath:markIndexPath];
    }
}

@end
